import datetime

from django.db.models import Q
from django.http import JsonResponse

from house.models import House
from libs.qiniuyun.qiniu_storage import QINIUYUN_URL
from order.models import Order
from users.models import User


# 根据房屋查询集获取房屋数据函数
def guolv(houses, p):
    """

    :param houses: 房屋查询集
    :param p: 页码
    :return:
    """
    house_list = []
    for house in houses:
        user = User.objects.get(id=house.user_id)  # 房东
        avatar_url = QINIUYUN_URL + str(user.avatar)
        house_list.append({
            "address": house.address,
            "area_name": house.area.name,
            "ctime": house.create_time,
            "house_id": house.id,
            "img_url": QINIUYUN_URL + str(house.index_image_url),
            "order_count": house.order_count,
            "price": house.price,
            "room_count": house.room_count,
            "title": house.title,
            "user_avatar": avatar_url
        })
    if len(house_list) == 0:
        return JsonResponse({
            "errmsg": "无数据",
            "errno": "4002"
        })
    data_dir = {'houses': house_list, "total_page": p}
    return JsonResponse({"data": data_dir,
                         "errmsg": "请求成功",
                         "errno": "0"
                         })


# 封装一个搜索函数
def sousuo(aid, sd, ed, pages, order_by):
        # 根据时间，区域搜索
        if all([aid, sd, ed]):
            sd = datetime.datetime.strptime(sd, '%Y-%m-%d')
            ed = datetime.datetime.strptime(ed, '%Y-%m-%d')

            orders = Order.objects.exclude(Q(begin_date__gte=ed) or Q(end_date__lte=sd))
            # houses = House.objects.filter(area_id=aid)
            ex_house_id_list = []
            for order in orders:
                ex_house_id_list.append(order.house_id)
            ex_house_id_list = list(set(ex_house_id_list))
            houses = House.objects.filter(area_id=aid).exclude(id__in=ex_house_id_list).order_by(order_by)
            return guolv(houses, pages)

        # 只搜索区域
        elif aid:
            houses = House.objects.filter(area_id=aid).order_by(order_by)
            return guolv(houses, pages)

        # 只搜索时间
        elif all([sd, ed]):
            sd = datetime.datetime.strptime(sd, '%Y-%m-%d')
            ed = datetime.datetime.strptime(ed, '%Y-%m-%d')

            orders = Order.objects.exclude(Q(begin_date__gte=ed) or Q(end_date__lte=sd))
            # houses = House.objects.filter(area_id=aid)
            ex_house_id_list = []
            for order in orders:
                ex_house_id_list.append(order.house_id)
            ex_house_id_list = list(set(ex_house_id_list))
            houses = House.objects.all().exclude(id__in=ex_house_id_list).order_by(order_by)
            return guolv(houses, pages)


        # 搜索所有房屋信息
        elif (not aid) and (not sd) and (not ed):
            houses = House.objects.all().order_by(order_by)
            return guolv(houses, pages)

        else:
            return JsonResponse({
                "errmsg": "缺少必要搜索参数",
                "errno": "4103"
            })
